//
//  XCOriginalView.swift
//  XCMicroBo
//
//  Created by 1 on 16/3/11.
//  Copyright © 2016年 1. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

//控件之间的间距
class XCOriginalView: UIView {
    
    //定义一个约束对象  用来更新约束
    var bottomConstraint: Constraint?


    //定义一个模型属性  用来给各个数据赋值
    var model:status?{
        
        didSet{
//        
            iconImageView.sd_setImageWithURL(model?.user?.headImageURL)
            mbrank_image.image = model?.user?.mbrank_image
            verified_image.image = model?.user?.verified_type_image
//
            nameLabel.text = model?.user?.name
//            timeLabel.text = model?.created_at
//            sourceLabel.text = model?.source
            contentLabel.text = model?.text
            
            
            //给图片视图传递数据
            pictureView.imageURLs = model?.imageURLs
            
            //卸载约束
            self.bottomConstraint?.uninstall()
            
//            
//            if let url1 = model?.imageURLs where url1.count != 0 {
//            
//                
//            }
//
//            //更新约束 当图片为0时  则原创微博的底部以contentLabel为基准
            if  let url = model?.imageURLs where  url.count != 0 {
            
                pictureView.hidden = false

                //设置iconImageView的imageURLs
                pictureView.imageURLs = url
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })

                
                
            }else {
                
                 //当图片不为0时  则原创微博的底部以iconImageView为基准
                pictureView.hidden = true
                
                //将imageURLs设置为nil 防止cell的重复利用
                pictureView.imageURLs = nil
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstraint = make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })

            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置背景颜色
        backgroundColor = UIColor.redColor()
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    private func setupUI() {
    
        //添加控件
        addSubview(iconImageView)
        addSubview(mbrank_image)
        addSubview(verified_image)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        //添加图片视图
        addSubview(pictureView)
        addSubview(contentLabel)

        
        //添加约束
        iconImageView.snp_makeConstraints { (make) -> Void in
            
            make.left.top.equalTo(self).offset(statusMagin)
            
            make.size.equalTo(CGSizeMake(36, 36))
        }
        
        mbrank_image.snp_makeConstraints { (make) -> Void in
            
//            make.right.bottom.equalTo(iconImageView)
            make.right.equalTo(iconImageView.snp_right)
            make.bottom.equalTo(iconImageView.snp_bottom)
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(iconImageView.snp_right).offset(statusMagin)
            
            make.top.equalTo(iconImageView.snp_top)
        }
        
        
        verified_image.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(nameLabel.snp_right).offset(statusMagin)
            make.top.equalTo(nameLabel.snp_top)
        }
        
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(nameLabel.snp_left)
            make.top.equalTo(nameLabel.snp_bottom).offset(statusMagin)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            
            make.left.equalTo(timeLabel.snp_right).offset(statusMagin)
            make.top.equalTo(nameLabel.snp_bottom).offset(statusMagin)
        }
        
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconImageView.snp_bottom).offset(statusMagin)
            make.left.equalTo(iconImageView.snp_left)
            make.right.equalTo(self.snp_right).offset(-statusMagin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(10)
            make.left.equalTo(contentLabel.snp_left)
//            make.right.equalTo(iconImageView.snp_right)
            

            
            /*
            
            */
            
            self.snp_makeConstraints(closure: { (make) -> Void in
                
              self.bottomConstraint =  make.bottom.equalTo(pictureView.snp_bottom).constraint
            })
            
        }
        
        

        //用户头像圆角处理
        iconImageView.layer.cornerRadius = 18
        iconImageView.layer.masksToBounds = true
            }
    
    //懒加载
    lazy var iconImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    //是否为VIP
    lazy var  mbrank_image = UIImageView(image: UIImage(named: "avatar_vip"))
    
    //用户认证类型
    lazy var verified_image = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    //图片视图
    lazy var pictureView = XCPictureView()
    
    lazy var nameLabel : UILabel = UILabel.labelWith("nameLabel", textColor: UIColor.grayColor(), textFont: 16)
    lazy var timeLabel : UILabel = UILabel.labelWith("timeLabel", textColor: UIColor.darkGrayColor(), textFont: 13)
    lazy var sourceLabel : UILabel = UILabel.labelWith("sourceLabel", textColor: UIColor.darkGrayColor(), textFont: 13)
    
//    lazy var contentLabel : UILabel = UILabel.labelWith("contentLabel", textColor: UIColor.darkGrayColor(), textFont: 14)

    lazy var contentLabel :UILabel = UILabel.labelWith("contentLabel", textColor: UIColor.darkGrayColor(), textAlignment:.Left , textFont: 14)
}

    
